<style>
    .layui-form .layui-form-item .layui-inline .layui-form-label {
        width: 100px;
    }
</style>
<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label" >Title：</label>
            <div class="layui-input-inline seller-inline-3" style="width:250px;">
                <input type="text" name="title" placeholder="title" autocomplete="off"
                       maxlength="50" class="layui-input" >
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" >desc：</label>
            <div class="layui-input-inline seller-inline-3" style="width:250px;">
                <input type="text" name="desc" placeholder="desc" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>Search</button>
                <a href="{:url('setAdd')}" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe608;</i>Add</a>
            </div>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="dataTable" lay-filter="dataTable" ></table>
</div>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch"
           lay-text="ON|OFF" lay-filter="change_status" {{ d.is_top == '1' ? 'checked' : '' }}>
</script>
<script>
    layui.use(['form',  'table'], function () {
        var tableIn = layui.table.render({
            elem: '#dataTable',
            height: 'full-99',
            cellMinWidth: '80',
            url: "{:url('message/index')}",
            method:'POST',
            cols: [[
                { type: 'numbers'},
                { field: 'ctime', align: 'center', title: 'Create Time' , width: 180,
                    templet: function (d) {
                        return layui.util.toDateString(d.ctime * 1000, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { align: 'center',  title: 'Is_top',toolbar: '#status',width: 100},
                { field: 'title', align: 'left',  title: 'Title', width: 200},

                { field: 'content', align: 'left', title: 'Content'},
                { title: 'action',width:200,
                    templet: function (d) {
                        var html= '<a class="layui-btn layui-btn-xs" href="{:url(\'setEdit\')}?id=' + d.id + '">Edit</a>';
                        html += '<a class="layui-btn layui-btn-xs layui-btn-danger option-del" data-id="' + d.id + '" >Del</a>';
                        return html;
                    }, align: 'center'
                }
            ]],
            page: true,
            limit: 20,
            limits: [20, 50, 100],
        });
        layui.form.on('switch(change_status)', function(obj){
            layer.confirm('Are you sure?', {
                title:'Window' , btn: ['confirm', 'cancel'], //按钮
            }, function () {
                JsPost("{:url('message/editStatus')}", {id:obj.value}, function(res){
                    if(res.code >0){
                        layer.msg(res.msg, { time: 1500 , icon:1}, function () {
                            tableIn.reload({
                                page:{
                                    curr :1
                                }
                            });
                        });
                    }else{
                        layer.msg(res.msg , {time:1500 , icon:2});
                    }
                })
            }, function () {
                tableIn.reload({
                    page:{
                        curr :1
                    }
                });
            });
        });
        layui.form.on('submit(search)', function (data) {
            layui.table.reload('dataTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'POST'
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //删除
        $(document).on('click', '.option-del', function () {
            var id = $(this).attr('data-id');
            layer.confirm('Are you sure you want to delete it?', {
                title:'Window' , btn: ['confirm', 'cancel'], //按钮
            }, function () {
                //去删除
                JsPost("{:url('delMsg')}", { 'id': id }, function (res) {
                    if (res.code) {
                        layer.msg('successfully delete.');
                        layui.table.reload('dataTable');
                    } else {
                        layer.msg('fail to delete');
                    }
                });
            }, function () {
                layer.close(1);
            });
        });
    });
</script>